package com.czy.service.impl;

import com.czy.dto.AddAssetDto;
import com.czy.dto.AssetPageDto;
import com.czy.entity.Asset;
import com.czy.mapper.AssetMapper;
import com.czy.service.AssetService;
import com.czy.utils.BeanCopyUtils;
import com.czy.vo.AdminAssetPageVo;
import com.czy.vo.AssetPageVo;
import com.czy.vo.PageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AssetServiceImpl implements AssetService {

    @Autowired
    private AssetMapper assetMapper;

    /**
     * 分页查询资产表
     */
    @Override
    public PageVo list(AssetPageDto assetPageDto) {
        //设置分页
        PageHelper.startPage(assetPageDto.getPage(), assetPageDto.getPageSize());

        //执行分页查询
        List<AssetPageVo> assetList = assetMapper.page(assetPageDto);

        //进行vo转换
        Page<AssetPageVo> page = (Page<AssetPageVo>) assetList;

        return new PageVo(page.getTotal(), page.getResult());
    }


    /**
     * 添加资产
     */
    @Override
    public void addAsset(AddAssetDto addAssetDto) {

        //拷贝
        Asset asset = BeanCopyUtils.copyBean(addAssetDto, Asset.class);

        // 补全基础属性，修改时间
        asset.setUpdateTime(LocalDateTime.now());
        asset.setCategoryId(addAssetDto.getCategoryName());

        System.out.println(asset);

        //执行SQL，添加资产信息
        assetMapper.insertAsset(asset);
    }

    /**
     * 根据id删除资产
     */
    @Override
    public void deleteAsset(ArrayList<Integer> ids) {
        assetMapper.deleteById(ids);
    }

    /**
     * 根据id查询资产信息
     */
    @Override
    public AdminAssetPageVo findById(Long id) {
        return assetMapper.findById(id);
    }
}
